<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>plugins/viewerpreferences.js - Documentation</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li><li><a href="global.html#viewerPreferences">viewerPreferences</a></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">plugins/viewerpreferences.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * jsPDF viewerPreferences Plugin
 * @author Aras Abbasi (github.com/arasabbasi)
 * Licensed under the MIT License.
 * http://opensource.org/licenses/mit-license
 */

 /**
 * Adds the ability to set ViewerPreferences and by thus
 * controlling the way the document is to be presented on the
 * screen or in print.
 */

(function (jsPDFAPI) {
    "use strict";
    /**
     * Set the ViewerPreferences of the generated PDF
     *
     * @param {Object} options Array with the ViewPreferences&lt;br />
     * Example: doc.viewerPreferences({"FitWindow":true});&lt;br />
     * &lt;br />
     * You can set following preferences:&lt;br />
     * &lt;br/>
     * &lt;b>HideToolbar&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false&lt;br />
     * &lt;br />
     * &lt;b>HideMenubar&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false.&lt;br />
     * &lt;br />
     * &lt;b>HideWindowUI&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false.&lt;br />
     * &lt;br />
     * &lt;b>FitWindow&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false.&lt;br />
     * &lt;br />
     * &lt;b>CenterWindow&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false&lt;br />
     * &lt;br />
     * &lt;b>DisplayDocTitle&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false.&lt;br />
     * &lt;br />
     * &lt;b>NonFullScreenPageMode&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: UseNone, UseOutlines, UseThumbs, UseOC&lt;br />
     * Default value: UseNone&lt;br/>
     * &lt;br />
     * &lt;b>Direction&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: L2R, R2L&lt;br />
     * Default value: L2R.&lt;br />
     * &lt;br />
     * &lt;b>ViewArea&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: MediaBox, CropBox, TrimBox, BleedBox, ArtBox&lt;br />
     * Default value: CropBox.&lt;br />
     * &lt;br />
     * &lt;b>ViewClip&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: MediaBox, CropBox, TrimBox, BleedBox, ArtBox&lt;br />
     * Default value: CropBox&lt;br />
     * &lt;br />
     * &lt;b>PrintArea&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: MediaBox, CropBox, TrimBox, BleedBox, ArtBox&lt;br />
     * Default value: CropBox&lt;br />
     * &lt;br />
     * &lt;b>PrintClip&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: MediaBox, CropBox, TrimBox, BleedBox, ArtBox&lt;br />
     * Default value: CropBox.&lt;br />
     * &lt;br />
     * &lt;b>PrintScaling&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: AppDefault, None&lt;br />
     * Default value: AppDefault.&lt;br />
     * &lt;br />
     * &lt;b>Duplex&lt;/b> &lt;i>(String)&lt;/i>&lt;br />
     * Possible values: Simplex, DuplexFlipLongEdge, DuplexFlipShortEdge
     * Default value: none&lt;br />
     * &lt;br />
     * &lt;b>PickTrayByPDFSize&lt;/b> &lt;i>(boolean)&lt;/i>&lt;br />
     * Default value: false&lt;br />
     * &lt;br />
     * &lt;b>PrintPageRange&lt;/b> &lt;i>(Array)&lt;/i>&lt;br />
     * Example: [[1,5], [7,9]]&lt;br />
     * Default value: as defined by PDF viewer application&lt;br />
     * &lt;br />
     * &lt;b>NumCopies&lt;/b> &lt;i>(Number)&lt;/i>&lt;br />
     * Possible values: 1, 2, 3, 4, 5&lt;br />
     * Default value: 1&lt;br />
     * &lt;br />
     * For more information see the PDF Reference, sixth edition on Page 577
     * @param {boolean} doReset True to reset the settings
     * @function
     * @returns jsPDF
     * @methodOf jsPDF#
     * @example
     * var doc = new jsPDF()
     * doc.text('This is a test', 10, 10)
     * doc.viewerPreferences({'FitWindow': true}, true)
     * doc.save("viewerPreferences.pdf")
     *
     * // Example printing 10 copies, using cropbox, and hiding UI.
     * doc.viewerPreferences({
     *   'HideWindowUI': true,
     *   'PrintArea': 'CropBox',
     *   'NumCopies': 10
     * })
     * @name viewerPreferences
     */
    jsPDFAPI.viewerPreferences = function (options, doReset) {
        options = options || {};
        doReset = doReset || false;

        var configuration;
        var configurationTemplate = {
            "HideToolbar": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.3},
            "HideMenubar": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.3},
            "HideWindowUI": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.3},
            "FitWindow": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.3},
            "CenterWindow": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.3},
            "DisplayDocTitle": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.4},
            "NonFullScreenPageMode": {defaultValue: "UseNone", value: "UseNone", type: "name", explicitSet: false, valueSet: ["UseNone", "UseOutlines", "UseThumbs", "UseOC"], pdfVersion: 1.3},
            "Direction": {defaultValue: "L2R", value: "L2R", type: "name", explicitSet: false, valueSet: ["L2R", "R2L"], pdfVersion: 1.3},
            "ViewArea": {defaultValue: "CropBox", value: "CropBox", type: "name", explicitSet: false, valueSet: ["MediaBox", "CropBox", "TrimBox", "BleedBox", "ArtBox"], pdfVersion: 1.4},
            "ViewClip": {defaultValue: "CropBox", value: "CropBox", type: "name", explicitSet: false, valueSet: ["MediaBox", "CropBox", "TrimBox", "BleedBox", "ArtBox"], pdfVersion: 1.4},
            "PrintArea": {defaultValue: "CropBox", value: "CropBox", type: "name", explicitSet: false, valueSet: ["MediaBox", "CropBox", "TrimBox", "BleedBox", "ArtBox"], pdfVersion: 1.4},
            "PrintClip": {defaultValue: "CropBox", value: "CropBox", type: "name", explicitSet: false, valueSet: ["MediaBox", "CropBox", "TrimBox", "BleedBox", "ArtBox"], pdfVersion: 1.4},
            "PrintScaling": {defaultValue: "AppDefault", value: "AppDefault", type: "name", explicitSet: false, valueSet: ["AppDefault", "None"], pdfVersion: 1.6},
            "Duplex": {defaultValue: "", value: "none", type: "name", explicitSet: false, valueSet: ["Simplex", "DuplexFlipShortEdge", "DuplexFlipLongEdge", "none"], pdfVersion: 1.7},
            "PickTrayByPDFSize": {defaultValue: false, value: false, type: "boolean", explicitSet: false, valueSet: [true, false], pdfVersion: 1.7},
            "PrintPageRange": {defaultValue: "", value: "", type: "array", explicitSet: false, valueSet: null, pdfVersion: 1.7},
            "NumCopies": {defaultValue: 1, value: 1, type: "integer", explicitSet: false, valueSet: null, pdfVersion: 1.7}
        };

        var configurationKeys = Object.keys(configurationTemplate);

        var rangeArray = [];
        var i = 0;
        var j = 0;
        var k = 0;
        var isValid = true;

        var method;
        var value;

        function arrayContainsElement(array, element) {
            var iterator;
            var result = false;

            for (iterator = 0; iterator &lt; array.length; iterator += 1) {
                if (array[iterator] === element) {
                    result = true;
                }
            }
            return result;
        }

        if (this.internal.viewerpreferences === undefined) {
            this.internal.viewerpreferences = {};
            this.internal.viewerpreferences.configuration = JSON.parse(JSON.stringify(configurationTemplate));
            this.internal.viewerpreferences.isSubscribed = false;
        }
        configuration = this.internal.viewerpreferences.configuration;

        if (options === "reset" || doReset === true) {
            var len = configurationKeys.length;

            for (k = 0; k &lt; len; k += 1) {
                configuration[configurationKeys[k]].value = configuration[configurationKeys[k]].defaultValue;
                configuration[configurationKeys[k]].explicitSet = false;
            }
        }

        if (typeof options === "object") {
            for (method in options) {
                value = options[method];
                if (arrayContainsElement(configurationKeys, method) &amp;&amp; value !== undefined) {

                    if (configuration[method].type === "boolean" &amp;&amp; typeof value === "boolean") {
                        configuration[method].value = value;
                    } else if (configuration[method].type === "name" &amp;&amp; arrayContainsElement(configuration[method].valueSet, value)) {
                        configuration[method].value = value;
                    } else if (configuration[method].type === "integer" &amp;&amp; Number.isInteger(value)) {
                        configuration[method].value = value;
                    } else if (configuration[method].type === "array") {

                        for (i = 0; i &lt; value.length; i += 1) {
                            isValid = true;
                            if (value[i].length === 1 &amp;&amp; typeof value[i][0] === "number") {
                                rangeArray.push(String(value[i]));
                            } else if (value[i].length > 1) {
                                for (j = 0; j &lt; value[i].length; j += 1) {
                                    if (typeof value[i][j] !== "number") {
                                        isValid = false;
                                    }
                                }
                                if (isValid === true) {
                                    rangeArray.push(String(value[i].join("-")));
                                }
                            }
                        }
                        configuration[method].value = String(rangeArray);
                    } else {
                        configuration[method].value = configuration[method].defaultValue;
                    }

                    configuration[method].explicitSet = true;
                }
            }
        }

        if (this.internal.viewerpreferences.isSubscribed === false) {
            this.internal.events.subscribe("putCatalog", function () {
                var pdfDict = [];
                var vPref;
                for (vPref in configuration) {
                    if (configuration[vPref].explicitSet === true) {
                        if (configuration[vPref].type === "name") {
                            pdfDict.push("/" + vPref + " /" + configuration[vPref].value);
                        } else {
                            pdfDict.push("/" + vPref + " " + configuration[vPref].value);
                        }
                    }
                }
                if (pdfDict.length !== 0) {
                    this.internal.write("/ViewerPreferences" + "&lt;&lt;\n" + pdfDict.join("\n") + "\n>>");
                }
            });
            this.internal.viewerpreferences.isSubscribed = true;
        }

        this.internal.viewerpreferences.configuration = configuration;
        return this;
    };
})(jsPDF.API);
</code></pre>
        </article>
    </section>




</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Sep 14 2017 20:43:25 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>
